let username, password, isCk;
var timestamp = new Date().getTime();
layui.config({
  version: '1510164124181'
}).use(['form', 'laypage', 'layer', 'element'], function () {
  form = layui.form;
  layer = layui.layer;
  $ = layui.jquery;
  laypage = layui.laypage;//分页
  element = layui.element; //元素操作
  let domain = document.domain;
  $("#isfollow option[value='" + domain + "']").attr("selected", "selected");
  username = getCookieValue("username");
  token = getCookieValue("token");
  let pad = getCookieValue("password");
  isCk = getCookieValue("isCk");

  //解密
  var passwords = Base64.decode(pad);
  password = passwords.substring(0, passwords.indexOf("_"));
  $("#AdminName").html(username);
  SelectMeauInfo();
  $(".InitialPage").attr("href", "indexs.html");
});
function changecodeimg(obj) {
  timestamp = new Date().getTime();
  $(obj).attr("src", httpurls + "/userverification?" + timestamp);
}
function gototarget() {
  var thisvals = $("#isfollow option:selected").val();
  if (thisvals == "wwwt.jiuhuakang.com") {
    httpurls = "https://aq.jiuhuakang.com";
    targeturl = "http://" + thisvals;
  } else if (thisvals == "wwwt.tlcha.com.cn") {
    httpurls = "https://qq.tlcha.com.cn";
    targeturl = "http://" + thisvals;
  } else if (thisvals == "wwwt.ygcha.com.cn") {
    httpurls = "https://aq.ygcha.com.cn";
    targeturl = "http://" + thisvals;
  } else if (thisvals == "wwwt.ruinuomei.com") {
    httpurls = "https://aq.ruinuomei.com";
    targeturl = "http://" + thisvals;
  }
  layer.open({
    type: 1,
    area: ['500px', '300px'],
    shadeClose: false, //点击遮罩关闭
    content: '<form class="am-form" style="width: 80%;margin: 0 auto;">' +
      '<br><br>' +
      '<div class="codeimg" style="font-size: 20px;    display: flex;">' +
      '<img style="margin: 0 auto;" onclick="changecodeimg(this)" src="' + httpurls + "/userverification?" + timestamp + '">' +
      '</div>' +
      '<br>' +
      '<div class="form-group-col-2">' +
      '<div class="form-cont" style="margin:0 auto">' +
      '<input class="form-control width100" id="code" type="text" value="" placeholder="请输入验证码" />' +
      '<span>如是密码不正确,请重新登录哟~~</span>' +
      '</div>' +
      '</div>' +
      '<br>' +
      '<div class="flex flex-wrap flex-col-3">' +

      '<div class="form-group-col-2">' +
      '<div class="form-label"></div>' +
      '<div class="form-cont">' +
      '<input type="button" onclick="returnLogin()" value="重新登录"  class="btn btn-info"/>&nbsp;&nbsp;' +
      '<input type="button" onclick="sure()" value="确认"  class="btn btn-info"/>&nbsp;&nbsp;' +
      '<input type="button" onclick="re()" value="取消" class="btn btn-danger"/>' +

      '</div>' +
      '</div>' +
      '</div>' +
      '</form>'
  });
}
function returnLogin() {
  layer.confirm('确定登出管理中心？', {
    title: '系统提示',
    btn: ['确定', '取消']
  }, function () {
    deleteCookie("token", "/");
    console.log(getCookieValue("token"));
    location.href = 'index.html';
  });
}
function sure() {
  var code = $("#code").val();
  $.ajax({
    type: "POST",
    url: httpurls + "/findUserName",
    cache: false,
    data: {
      "username": username,
      "password": password,
      "code": code,
      "isCk": isCk
    },
    success: function (data) {
      if (data.rtnCode == 200) {
        if (isCk == 0) {
          addCookie("token", data.msg, 1, "/");
          addCookie("username", username, 1, "/");
        } else {
          addCookie("token", data.msg, 7, "/");
          addCookie("username", username, 7, "/");
        }
        let pd = Base64.encode(password + "_1512350");
        window.location.href = targeturl + "/indexs.html";
      } else {
        layer.msg(data.msg);
        timestamp = new Date().getTime();
        $(".codeimg img").attr("src", httpurls + "/userverification?" + timestamp);
      }
    }, error: function (data) {
      layer.msg("读取接口失败,请稍后再试试");
    }
  })
}
function SelectMeauInfo() {
  $.ajax({
    url: httpurl + "/callMenuUser?token=" + GetTokenInfo(),
    cache: false,
    async: false,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Authorization": GetTokenInfo()
    },
    contentType: 'application/json',
    success: function (data) {
      console.log(data);
      if (data.rtnCode == 200) {
        let dataInfo = data.data;
        let info = "";
        for (let i = 0; i < dataInfo.length; i++) {
          info += '<li>' +
            '<dl>' +
            '<a target="right" href="' + dataInfo[i].url + '">' +
            '<dt><i class="icon-inbox"></i>' + dataInfo[i].menu_name + '<i class="icon-angle-right"></i></dt>' +
            '</a>';
          let ChildInfo = dataInfo[i].subdirectory;
          if (ChildInfo != null) {
            for (let a = 0; a < ChildInfo.length; a++) {
              info += '<a target="right" href="' + ChildInfo[a].url + '"><dd>' + ChildInfo[a].menu_name + '<i class="icon-angle-right"></i></dd></a>';
            }
          }
          info += '</dl></li>';
        }
        $("#AdminInfo").append(info);
      } else {
        layer.alert(data.msg);
        deleteCookie("token", "/");
        window.location.href = "index.html";
      }
    }
  });
}

